import { takeEvery } from "redux-saga/effects";
import { NATIVE_CLASS } from "@common/constants";
import { filterEmptyProperties } from '@common/utils';
import {
  SAGA_PROD_INBOUND_DEL_BARCODE,
  SAGA_PROD_INBOUND_FINISH_MIX_OPS,
  SAGA_PROD_INBOUND_FINISH_OPS,
  SAGA_PROD_INBOUND_LOAD_BARCODE_DETAILS,
  SAGA_PROD_INBOUND_LOAD_ITEM_DETAILS,
  SAGA_PROD_INBOUND_LOAD_MIX_BARCODE_DETAILS,
  SAGA_PROD_INBOUND_LOAD_MIX_ITEM_DETAILS,
  SAGA_PROD_INBOUND_LOAD_MIX_OPS_FORM_DATA,
  SAGA_PROD_INBOUND_LOAD_OPS_FORM_DATA,
  SAGA_PROD_INBOUND_LOAD_REC_LOC,
  SAGA_PROD_INBOUND_PUSH_2_SERVER,
  SAGA_PROD_INBOUND_STASH_MIX_OPS_DATA,
  SAGA_PROD_INBOUND_STASH_OPS_DATA,
} from "./saga-types";
import { callNativeFunc } from "@common/saga-helper";
import {
  ADD_PROD_INBOUND_ACC_QTY,
  DEL_PROD_INBOUND_BARCODE,
  SET_PROD_INBOUND_BARCODE_DETAILS,
  SET_PROD_INBOUND_ITEM_DETAILS,
  SET_PROD_INBOUND_OPS_FORM_DATA,
  SET_PROD_INBOUND_REC_LOC,
} from "./action-types";

function* loadProdInboundOpsFormDataSaga() {
  yield takeEvery(SAGA_PROD_INBOUND_LOAD_OPS_FORM_DATA, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.ProdInboundDetailsHandler,
            action: "loadOpsFormData",
            needNonce: false,
            params: action.payload,
          },
          respActions: [SET_PROD_INBOUND_OPS_FORM_DATA],
        },
      ],
    });
  });
}

function* stashProdInboundOpsDataSaga() {
  yield takeEvery(SAGA_PROD_INBOUND_STASH_OPS_DATA, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.ProdInboundOpsHandler,
            action: "create",
            needNonce: false,
            params: action.payload,
          },
          respActions: [ADD_PROD_INBOUND_ACC_QTY],
          callback: action.payload.callback,
        },
      ],
    });
  });
}

function* finishProdInboundOpsSaga() {
  yield takeEvery(SAGA_PROD_INBOUND_FINISH_OPS, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.ProdInboundOpsHandler,
            action: "finish",
            needNonce: false,
            params: action.payload,
          },
          callback: action.payload.callback,
        },
      ],
    });
  });
}

function* loadProdInboundItemDetailsSaga() {
  yield takeEvery(SAGA_PROD_INBOUND_LOAD_ITEM_DETAILS, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.ProdInboundDetailsHandler,
            action: "loadItemDetails",
            needNonce: false,
            params: action.payload,
          },
          respActions: [SET_PROD_INBOUND_ITEM_DETAILS],
        },
      ],
    });
  });
}

function* loadProdInboundBarcodeDetailsSaga() {
  yield takeEvery(SAGA_PROD_INBOUND_LOAD_BARCODE_DETAILS, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.ProdInboundOpsHandler,
            action: "loadBarcodeDetails",
            needNonce: false,
            params: action.payload,
          },
          respActions: [SET_PROD_INBOUND_BARCODE_DETAILS],
        },
      ],
    });
  });
}

function* delProdInboundBarcodeSaga() {
  yield takeEvery(SAGA_PROD_INBOUND_DEL_BARCODE, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.ProdInboundOpsHandler,
            action: "delBarcode",
            needNonce: false,
            params: action.payload,
          },
          respActions: [DEL_PROD_INBOUND_BARCODE],
          callback: action.payload.callback
        },
      ],
    });
  });
}

function* pushProdInbound2ServerSaga() {
  yield takeEvery(SAGA_PROD_INBOUND_PUSH_2_SERVER, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.ProdInboundHandler,
            action: "push2Server",
            needNonce: true,
            params: action.payload,
          },
          callback: action.payload.callback
        },
      ],
    });
  });
}

function* loadProdInboundMixOpsFormDataSaga() {
  yield takeEvery(SAGA_PROD_INBOUND_LOAD_MIX_OPS_FORM_DATA, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.ProdMixInboundHandler,
            action: "loadOpsFormData",
            needNonce: false,
            params: filterEmptyProperties(action.payload),
          },
          respActions: [SET_PROD_INBOUND_OPS_FORM_DATA]
        },
      ],
    });
  });
}

function* loadProdInboundMixRecLocSaga() {
  yield takeEvery(SAGA_PROD_INBOUND_LOAD_REC_LOC, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.ProdMixInboundHandler,
            action: "loadSuListByBarcode",
            needNonce: false,
            params: action.payload
          },
          respActions: [SET_PROD_INBOUND_REC_LOC],
          callback: action.payload.callback
        },
      ],
    });
  });
}

function* stashProdInboundMixOpsDataSaga() {
  yield takeEvery(SAGA_PROD_INBOUND_STASH_MIX_OPS_DATA, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.ProdMixInboundOpsHandler,
            action: "create",
            needNonce: false,
            params: filterEmptyProperties(action.payload)
          },
          callback: action.payload.callback
        },
        {
          buildPayload: (resps) => ({
            classFullName: NATIVE_CLASS.ProdInboundDetailsHandler,
            action: "loadOpsFormData",
            needNonce: false,
            params: {id: resps[0].payload.id}
          }),
          respActions: [SET_PROD_INBOUND_OPS_FORM_DATA]
        }
      ],
    });
  });
}

function* loadProdInboundMixItemDetailsSaga() {
  yield takeEvery(SAGA_PROD_INBOUND_LOAD_MIX_ITEM_DETAILS, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.ProdMixInboundHandler,
            action: "loadItemDetails",
            needNonce: false,
            params: filterEmptyProperties(action.payload)
          },
          respActions: [SET_PROD_INBOUND_ITEM_DETAILS],
        },
      ],
    });
  });
}

function* loadProdInboundMixBarcodeDetailsSaga() {
  yield takeEvery(SAGA_PROD_INBOUND_LOAD_MIX_BARCODE_DETAILS, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.ProdMixInboundHandler,
            action: "loadBarcodeDetails",
            needNonce: false,
            params: filterEmptyProperties(action.payload)
          },
          respActions: [SET_PROD_INBOUND_BARCODE_DETAILS],
        },
      ],
    });
  });
}

function* finishProdInboundMixOpsSaga() {
  yield takeEvery(SAGA_PROD_INBOUND_FINISH_MIX_OPS, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.ProdMixInboundOpsHandler,
            action: "finish",
            needNonce: false,
            params: filterEmptyProperties(action.payload),
          },
          callback: action.payload.callback,
        },
      ],
    });
  });
}

export default [
  loadProdInboundOpsFormDataSaga(),
  stashProdInboundOpsDataSaga(),
  finishProdInboundOpsSaga(),
  loadProdInboundItemDetailsSaga(),
  loadProdInboundBarcodeDetailsSaga(),
  delProdInboundBarcodeSaga(),
  pushProdInbound2ServerSaga(),

  loadProdInboundMixOpsFormDataSaga(),
  loadProdInboundMixRecLocSaga(),
  stashProdInboundMixOpsDataSaga(),
  loadProdInboundMixItemDetailsSaga(),
  loadProdInboundMixBarcodeDetailsSaga(),
  finishProdInboundMixOpsSaga()
];
